package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据：{}", shoppingCart);

        //绑定用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        //设置用户id判断条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);

        //判断当前添加的是菜品还是套餐
        if (dishId != null) {
            //比较菜品id
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //比较套餐id
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //通过判断条件进行搜索
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);

        //判断当前是否存在数据
        if (cartServiceOne != null) {
            //将数量加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //设置默认数量为一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }

    /**
     * 减少购物数
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        //获取菜品id
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //绑定user对象的id
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        if (dishId != null) {
            //比较菜品id
            lqw.eq(ShoppingCart::getDishId,dishId);
        } else {
            //比较套餐id
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //获取购物车对象
        ShoppingCart one = shoppingCartService.getOne(lqw);
        //获取该对象数量Number
        Integer number = one.getNumber();
        //判断是否为空
        if (number == 0){
            throw new CustomException("当前数量已为0，不可再减");
        }
        if (number != 1) {
            //将数量减一
            one.setNumber(number - 1);
            shoppingCartService.updateById(one);
        }  else {
            //直接删除该对象
            shoppingCartService.removeById(one);
        }

        return R.success("修改成功");
    }


    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车");

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }
}
